GBML manual / Arrays

array_sort

Puts the places of target in order. Numbers go smallest first when ascending is true and biggest first when it is false. Text goes in alphabetical order when ascending is true (Z to A when it is false), with one catch: every capital letter comes before every small letter. It changes target itself and makes no copy. An array of yes-or-no values, structs or references is refused when the project is checked.

Syntax

array_sort(target, ascending)

Arguments

NameType
targetarray
ascendingbool

Returns

Nothing.

Description

Puts the places of target, an array, in order. For numbers, when ascending is true the smallest comes first, and when it is false the biggest comes first. For text, true puts the A's first and false puts the Z's first (there is a catch about capital letters, below).

array_sort(heights, true);    // smallest first
array_sort(heights, false);   // biggest first
array_sort(words, true);      // A first

It changes the array itself

array_sort rearranges the values that are already in target. It does not make a new array, and it needs no extra memory. The old order is gone afterwards. To keep it, make a copy with array_copy first and sort the one you want sorted.

It sorts the places the array really has, the number array_length gives you. An array never gains or loses a place, so it has exactly as many places afterwards.

Numbers and text

An array of whole numbers or decimals is put in number order, and an array of text is put in alphabetical order. An array of yes-or-no values, of structs or of references is refused when the project is checked, before the game runs: GBML does not know what order those should go in, so it will not guess.

The array holdsWhat happens
whole numbers (array<int>)sorted
decimals (array<real>)sorted
text (array<string>)sorted, in the order below
yes-or-no values, structs or referencesrefused when checked

If you only want the order mixed up, array_shuffle works on an array of anything.

Text goes in alphabetical order, with a catch

Text is put in order one character at a time, by each character's code. The code is the number a character has in the table of letters ("A" is 65, "a" is 97, "0" is 48, a space is 32: see string_code_at). That gives these rules:

RuleExample
a space comes before a digit, a digit before a capital letter, a capital letter before a small letter" x", "10", "Zed", "ada" come in that order
every capital letter comes before every small letter. A capital and a small letter are not treated as the same letter"Zed" comes before "ada", and "Apple" before "apple"
digits are compared one at a time"10" comes before "9", because 1 comes before 9
a text that is the start of a longer one comes first"ab" comes before "abc"
a character past the plain keyboard ones (a code above 127) comes after all of themit comes last

If you want names to come out the way a dictionary would put them, write them all with a capital first letter, or all small. With ascending set to false the whole order is turned around.

What happens in the awkward cases

You do thisWhat happens
sort an array that has equal numbers, or equal pieces of text, in itthey end up next to each other. Two equal values are the same value, so it does not matter which is which
leave ascending out, or give a number instead of true or falserefused when checked
give it a list or a gridrefused when checked, and the message names the function that sorts that kind, like list_sort
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_sort inside a script sorts the caller's array, whatever its size.

What it costs

Nothing is set aside: the sort is done in place, one value at a time. The time grows a little faster than the number of places, and comparing two pieces of text takes longer than comparing two numbers. No timing was measured.

Example

var heights = array_create(4, 0);
heights[0] = 30;
heights[1] = 10;
heights[2] = 40;
heights[3] = 20;
array_sort(heights, true);    // 10, 20, 30, 40
array_sort(heights, false);   // 40, 30, 20, 10
var words = array_create(3, "");
words[0] = "pear";
words[1] = "Zed";
words[2] = "ada";
array_sort(words, true);      // "Zed", "ada", "pear": a capital letter comes before a small one

On the GBA

Sorts the array_length(target) places the array has with a shell sort over the vector, in place. A shell sort done in place: it uses no extra memory, makes no copy, and never leaves the places that hold items. It is not stable, which cannot show for numbers or text (two equal values are the same value). The time grows a little faster than the number of items - about n times the square root of n in the worst case, usually much less - and comparing two texts takes longer than comparing two numbers (as many steps as the two texts have in common at the start). No timing was measured. Whole numbers, decimals and text build. Text is put in order one character at a time, by each character's code. A space comes before a digit, a digit before a capital letter and a capital letter before a small one, so "Zed" comes before "ada" (a capital and a small letter are NOT treated as the same letter), and a text that is the start of a longer one comes first ("ab" before "abc"). A character with a code above 127 comes after all the plain keyboard ones. There is no order for yes-or-no values, structs or references that GBML knows, so those are refused when the project is checked.

See also: list_sortgrid_sortgrid_sort_rowsarray_shufflestring_code_atarray_lengtharray_copyarray_foreachArraysData containers